package org.ling.xuan.gutter;


import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo;
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider;
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiExpressionList;
import com.intellij.psi.PsiMethodCallExpression;
import org.jetbrains.annotations.NotNull;
import org.ling.xuan.util.Icons;

import java.util.Collection;


public class ParamGutterIconProvider extends RelatedItemLineMarkerProvider {



    protected void collectNavigationMarkers(@NotNull PsiElement element,
                                            @NotNull Collection<? super RelatedItemLineMarkerInfo<?>> result) {
        if (match(element)) {
            PsiMethodCallExpression pse = (PsiMethodCallExpression) element;
            NavigationGutterIconBuilder<PsiElement> builder =
                    NavigationGutterIconBuilder.create(Icons.TB_PARAM)
                            .setTargets(element)
                            .setTooltipText(getParamInfo(element));
            result.add(builder.createLineMarkerInfo(pse));
        }
    }

    private boolean match(PsiElement element) {
        if (element instanceof PsiMethodCallExpression) {
            PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) element;
            String callText =methodCallExpression.getText();
            return callText.contains("ParamCache") && callText.contains("getInstance");
        }
        return false;
    }

    public String getParamInfo(@NotNull PsiElement element) {
        PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) element;
        PsiExpressionList list=methodCallExpression.getArgumentList();
        String text = "";
        for( PsiExpression expression:list.getExpressions()){
            if(expression.getType()!=null&&expression.getType().getCanonicalText().contains("String")){
                text+=expression.getText()+"~";
            }
        }
        if(text.endsWith("~")){
            text = text.substring(0,text.length()-1);
        }
        return text;
    }

}
